package fhq.hcmute.edu.vn.activities;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ToggleButton;
import fhq.hcmute.edu.vn.adapter.MySettingAdapter;
import fhq.hcmute.edu.vn.adapter.SettingItem;
import fhq.hcmute.edu.vn.languages.Language;
import fhq.hcmute.edu.vn.util.SharePreferencesUtil.TouchPadSetting;
import fhq.hcmute.edu.vn.util.TAG;

public class TouchPadSettingActivity extends ListActivity {
	private String mouseSetting;
	private String touchpadSensitivity;
	private String touchpadSensitivityDescription;
	private String scrollSensitivity;
	private String scrollSensitivityDescription;
	private String ortherSetting;
	private String tapToClick;
	private String tapToClickDescription;
	private String leftHandMode;
	private String leftHandModeDescription;
	private String ScreenDimOff;
	private String screenTimeoutDescription;
	private List<SettingItem> myList;
	private static final int TOUCHPAD_SENSITIVITY = 1;
	private static final int SCROLL_SENSITIVITY = 2;
	private static final int TAP_TO_CLICK = 4;
	private static final int LEFT_HAND_MODE = 5;
	private static final int KEEP_SCREEN_ON = 6;
	private MySettingAdapter adapter;
	private boolean tgTapToClick = false;
	private boolean tgLeftHandMode = false;
	private boolean tgKeepScreenOn = false;

	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Language.updateLanguageSystem(getBaseContext());
		updateSetting();
		adapter = new MySettingAdapter(this, getSetting());
		setListAdapter(adapter);
	}

	private List<SettingItem> getSetting() {
		initLabel();
		myList = new ArrayList<SettingItem>();
		myList.add(new SettingItem(mouseSetting)); // title --------0
		myList.add(new SettingItem(touchpadSensitivity, // ---------1
				touchpadSensitivityDescription));
		myList.add(new SettingItem(scrollSensitivity,// -------------2
				scrollSensitivityDescription));
		myList.add(new SettingItem(ortherSetting)); // title -------3
		myList.add(new SettingItem(tapToClick, tapToClickDescription,
				tgTapToClick)); // -----4
		myList.add(new SettingItem(leftHandMode, leftHandModeDescription,
				tgLeftHandMode));// -----5
		myList.add(new SettingItem(ScreenDimOff, screenTimeoutDescription,// ------6
				tgKeepScreenOn));
		return myList;
	}

	private void initLabel() {
		mouseSetting = getResources().getString(
				R.string.Label_Mouse_Setting);
		touchpadSensitivity = getResources().getString(
				R.string.Label_TouchPad_Sensitivity);
		touchpadSensitivityDescription = getResources().getString(
				R.string.Label_TouchPad_Sensitivity_Description);
		scrollSensitivity = getResources().getString(
				R.string.Label_Scroll_Sensitivity);
		scrollSensitivityDescription = getResources().getString(
				R.string.Label_Scroll_Sensitivity_Description);
		ortherSetting = getResources().getString(
				R.string.Label_Orther_Setting);
		tapToClick = getResources().getString(
				R.string.Label_Tap_Click);
		tapToClickDescription = getResources().getString(
				R.string.Label_Tap_Click_Description);
		leftHandMode = getResources().getString(
				R.string.Label_Left_Hand);
		leftHandModeDescription = getResources().getString(
				R.string.Label_Left_Hand_Description);
		ScreenDimOff = getResources().getString(
				R.string.Label_Screen_Timeout);
		screenTimeoutDescription = getResources().getString(
				R.string.Label_Screen_Timeout_Description);
	}

	@Override
	protected final void onListItemClick(ListView l, View v, int position,
			long id) {
		Intent intent = null;
		switch (position) {
		case TOUCHPAD_SENSITIVITY:
			intent = new Intent(getApplicationContext(),
					TouchPadSensitivitySettingActivity.class);
			startActivity(intent);
			break;
		case SCROLL_SENSITIVITY:
			intent = new Intent(getApplicationContext(),
					MouseScrollSensitivitySettingActivity.class);
			startActivity(intent);
			break;
		}
		super.onListItemClick(l, v, position, id);
	}

	private void saveSetting() {
		for (int i = 0; i < getListView().getChildCount(); i++) {
			View row = getListView().getChildAt(i);
			ToggleButton toggleButton = (ToggleButton) row
					.findViewById(R.id.toggleButton);
			if (toggleButton != null) {
				boolean isCheck = toggleButton.isChecked();
				switch (i) {
				case TAP_TO_CLICK:
					saveToSharedPreferences(TouchPadSetting.TAP_TO_CLICK,
							isCheck);
					break;
				case LEFT_HAND_MODE:
					saveToSharedPreferences(TouchPadSetting.LEFT_HAND_MODE,
							isCheck);
					break;
				case KEEP_SCREEN_ON:
					saveToSharedPreferences(TouchPadSetting.KEEP_SCREEN_ON,
							isCheck);
					break;
				}
			}
		}
	}

	private void updateSetting() {
		SharedPreferences preferences =
				getSharedPreferences(TouchPadSetting.TOUCHPAD_SETTING,
						Context.MODE_PRIVATE);
		tgKeepScreenOn =
				preferences.getBoolean(TouchPadSetting.KEEP_SCREEN_ON, false);
		tgLeftHandMode =
				preferences.getBoolean(TouchPadSetting.LEFT_HAND_MODE, false);
		tgTapToClick =
				preferences.getBoolean(TouchPadSetting.TAP_TO_CLICK, false);
		Log.v(TAG.NAME, tgKeepScreenOn + " " + tgLeftHandMode + " "
				+ tgTapToClick);
	}

	@Override
	protected void onResume() {
		updateSetting();
		adapter.getList().get(LEFT_HAND_MODE).setChecked(tgLeftHandMode);
		adapter.getList().get(KEEP_SCREEN_ON).setChecked(tgKeepScreenOn);
		adapter.getList().get(TAP_TO_CLICK).setChecked(tgTapToClick);
		adapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	protected void onPause() {
		saveSetting();
		super.onPause();
	}

	private void saveToSharedPreferences(String key, boolean value) {
		SharedPreferences preferences =
				getSharedPreferences(TouchPadSetting.TOUCHPAD_SETTING, 0);
		SharedPreferences.Editor editor = preferences.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
}
